/***************************************************************************
 *   Copyright (C) 2009 by p1r0                                            *
 *   lucifers.preacher@gmail.com                                           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <iostream>
#include "files.h"
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <unistd.h>
#include <curses.h>

#include "corefunctions.h"
#include "conf.h"

Handle<Value> IncludeCallback(const Arguments& args) 
{
    if (args.Length() < 1) return v8::Undefined();
    HandleScope scope;
    Handle<Value> arg = args[0];
    String::Utf8Value value(arg);
    char * contents;
    if(Files::load(*value, contents))
    {

        v8::Handle<v8::Script> script = v8::Script::Compile(String::New(contents));
        delete[] contents;
        if (script.IsEmpty()) 
        {
            // Print errors that happened during compilation.
            
        } 
        else 
        {
            v8::Handle<v8::Value> result = script->Run();
            if (result.IsEmpty()) 
            {
                // Print errors that happened during execution.
               
                
            } 
            else 
            {
               
               
            }
        }
    }
    else
    {
        cout << "Cannot include: " << *value << ". File not found" << endl;
    }
    
    

    return v8::Undefined();
}

Handle<Value> PrintCallback(const Arguments& args) 
{
    if (args.Length() < 1) return v8::Undefined();
    HandleScope scope;
    Handle<Value> arg = args[0];
    String::Utf8Value value(arg);
    
    cout << *value;

    return v8::Undefined();
}

Handle<Value> GetLineCallback(const Arguments& args) 
{
    string tmp = "";
    getline(cin, tmp);
    return String::New(tmp.c_str());
}

Handle<Value> ClearCallback(const Arguments& args) 
{
    system(CLEAR_COMMAND); 
    return v8::Undefined();
}

Handle<Value> CallCallback(const Arguments& args) 
{
    //This function gives direct access to the
    //system so if jail mode is on return inmediatly
    if(Conf::getInstance()->isJailed())
    {
        return v8::Undefined();
    }

    if (args.Length() < 1) return v8::Undefined();

    Handle<Value> arg = args[0];
    String::Utf8Value value(arg);
    string data;
    FILE *stream;
    char buffer[MAX_BUFFER];

    memset(buffer, '\0', MAX_BUFFER);
    // do it
    stream = popen(*value, "r");
    while ( fgets(buffer, MAX_BUFFER, stream) != NULL )
    {
        data.append(buffer);
    }
    pclose(stream);

    return String::New(data.c_str());
}

Handle<Value> SleepCallback(const Arguments& args) 
{
    if (args.Length() < 1) return v8::Undefined();
    int seconds = args[0]->Int32Value();
    sleep(seconds);
    return v8::Undefined();
}

Handle<Value> GetcharCallback(const Arguments& args) 
{
    string tmp = "";
    char c[2];
    c[0] = '\0';
    c[1] = '\0';

    c[0] = getchar();
    tmp = string(c);
    return String::New(tmp.c_str());
}
